package cn.edu.cdp.service.impl;

import cn.edu.cdp.dao.*;
import cn.edu.cdp.domain.*;
import cn.edu.cdp.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    private final AccountDao accountDao;
    private final CollegeDao collegeDao;
    private final CourseDao courseDao;
    private final MajorDao majorDao;
    private final StudentDao studentDao;
    private final AdminDao adminDao;

    @Autowired
    public AdminServiceImpl(AccountDao accountDao, CollegeDao collegeDao, CourseDao courseDao, MajorDao majorDao, StudentDao studentDao, AdminDao adminDao) {
        this.accountDao = accountDao;
        this.collegeDao = collegeDao;
        this.courseDao = courseDao;
        this.majorDao = majorDao;
        this.studentDao = studentDao;
        this.adminDao = adminDao;
    }

    @Override
    public List<Map<String, Object>> allAccount() {
        return accountDao.allAccount();
    }

    @Override
    public List<College> allCollege() {
        return collegeDao.allCollege();
    }

    @Override
    public List<Map<String, Object>> allCourse() {
        return courseDao.allCourse();
    }

    @Override
    public List<Map<String, Object>> allMajor() {
        return majorDao.allMajor();
    }

    @Override
    public List<Student> allStudent() {
        return studentDao.allStudent();
    }

    // 账户
    @Override
    public void accountInsert(Account account) {
        accountDao.insert(account);
    }

    @Override
    public void accountUpdate(Account account) {
        accountDao.update(account);
    }

    @Override
    public void accountDeleteByUsername(String username) {
        accountDao.deleteByUsername(username);
    }

    // 学院
    @Override
    public void collegeInsert(College college) {
        collegeDao.insert(college);
    }

    @Override
    public void collegeUpdate(College college) {
        collegeDao.update(college);
    }

    @Override
    public void collegeDeleteById(String collegeId) {
        collegeDao.deleteById(collegeId);
    }

    // 专业
    @Override
    public void majorInsert(String majorId, String name, String category, String collegeName) {
        String collegeId = collegeDao.getIdByName(collegeName);
        majorDao.insert(new Major(majorId, name, category, collegeId));
    }

    @Override
    public void majorUpdate(String majorId, String name, String category, String collegeName) {
        String collegeId = collegeDao.getIdByName(collegeName);
        majorDao.update(new Major(majorId, name, category, collegeId));
    }

    @Override
    public void majorDeleteById(String majorId) {
        majorDao.deleteById(majorId);
    }

    // 课程
    @Override
    public void courseInsert(String courseId, String name, Double credit, String semester, String collegeName) {
        String collegeId = collegeDao.getIdByName(collegeName);
        courseDao.insert(new Course(courseId, name, credit, semester, collegeId));
    }

    @Override
    public void courseUpdate(String courseId, String name, Double credit, String semester, String collegeName) {
        String collegeId = collegeDao.getIdByName(collegeName);
        courseDao.update(new Course(courseId, name, credit, semester, collegeId));
    }

    @Override
    public void courseDeleteById(String courseId) {
        courseDao.deleteById(courseId);
    }

    // 学生
    @Override
    public void studentInsert(String studentId, String name, String sex, Date birthday, Double ceeScore, String nation, String nativePlace, String majorName) {
        String majorId = majorDao.getIdByName(majorName);
        studentDao.insert(new Student(studentId, name, sex, birthday, ceeScore, nation, nativePlace, null, null, majorId, majorName));
    }

    @Override
    public void studentUpdate(String studentId, String name, String sex, Date birthday, Double ceeScore, String nation, String nativePlace, String majorName) {
        String majorId = majorDao.getIdByName(majorName);
        studentDao.update(new Student(studentId, name, sex, birthday, ceeScore, nation, nativePlace, null, null, majorId, majorName));
    }

    @Override
    public void studentDeleteById(String studentId) {
        studentDao.deleteById(studentId);
    }


    // 作业要求
    @Override
    public List<Map<String, Object>> scoreByStudentIdOrName(String text) {
        return adminDao.scoreByStudentIdOrName(text);
    }

    @Override
    public List<Map<String, Object>> statisticStudentNumberByMajor() {
        return adminDao.statisticStudentNumberByMajor();
    }

    @Override
    public List<Map<String, Object>> scoreByCourseIdOrName(String text) {
        return adminDao.scoreByCourseIdOrName(text);
    }

    @Override
    public List<Map<String, Object>> totalCredit() {
        return adminDao.totalCredit();
    }

    @Override
    public List<Map<String, Object>> failExam() {
        return adminDao.failExam();
    }

    @Override
    public List<Map<String, Object>> gainScholarship() {
        return adminDao.gainScholarship();
    }

    @Override
    public List<Student> filterStudent(String collegeName, String majorName, String studentIdOrName) {
        return adminDao.filterStudent(collegeName, majorName, studentIdOrName);
    }

    @Override
    public List<Major> filterMajorByCollegeName(String collegeName) {
        return majorDao.filterMajorByCollegeName(collegeName);
    }
}
